package pp.strategy.worktime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pp.IWorkTimeStrategy;
import pp.PowerProject;

import java.lang.reflect.Field;

public enum WorkTimeStrategyEnum {

    AUTO(AutoWorkTimeStrategy.class),
    HAND(HandWorkTimeStrategy.class),
    SMART(SmartWorkTimeStrategy.class),
    ALL_AUTO(AllAutoWorkTimeStrategy.class),
    ;
    private static final Logger logger = LoggerFactory.getLogger(WorkTimeStrategyEnum.class);

    private Class<? extends IWorkTimeStrategy> aClass;

    WorkTimeStrategyEnum(Class<? extends IWorkTimeStrategy> aClass) {
        this.aClass = aClass;
    }

    public static IWorkTimeStrategy newStrategy(String type, PowerProject pp) {
        for (WorkTimeStrategyEnum value : values()) {
            if (value.name().equalsIgnoreCase(type)) {
                try {
                    logger.info("工时使用[{}]模式", type);
                    IWorkTimeStrategy iWorkTimeStrategy = value.aClass.newInstance();
                    try {
                        Field pp1 = value.aClass.getDeclaredField("pp");
                        pp1.setAccessible(true);
                        pp1.set(iWorkTimeStrategy, pp);
                    } catch (NoSuchFieldException e) {

                    }
                    return iWorkTimeStrategy;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        throw new RuntimeException("没有这个[" + type + "]策略");
    }
}
